package q35_searchInsert;

public class Solution_2 {
    /*
    标准的二分查找
     */
    public static int searchInsert(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, ans = n;
        while (left <= right) {
            // 记录mid节点
            int mid = ((right - left) >> 1) + left;
            // 每次都更新ans
            if (target <= nums[mid]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;

    }
}
